package com.alipay.pay;

import com.alipay.bean.Refund;
import com.alipay.bean.RefundDetailData;
import com.alipay.config.AlipayConfig;
import com.alipay.util.AlipaySubmit;
import com.alipay.util.WebAlipaySubmit;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by cqd on 2015-11-25.
 */
@Service
public class AlipayService {

    /**
     * @param out_trade_no 订单号
     * @param subject      商品名称
     * @param total_fee    总价格
     * @param body         商品详情商品详情
     * @param notify_url   回调通知页面
     **/
    public String pay(String out_trade_no, String subject, String total_fee, String body, String notify_url) {
        String payment_type = "1";
        Map<String, String> sParaTemp = new HashMap<String, String>();
        sParaTemp.put("partner", "\"" + AlipayConfig.partner + "\"");
        sParaTemp.put("seller_id", "\"" + AlipayConfig.paymentAccount + "\"");
        sParaTemp.put("out_trade_no", "\"" + out_trade_no + "\"");
        sParaTemp.put("subject", "\"" + subject + "\"");
        sParaTemp.put("body", "\"" + body + "\"");
        sParaTemp.put("total_fee", "\"" + total_fee + "\"");
        sParaTemp.put("notify_url", "\"" + notify_url + "\"");
        sParaTemp.put("service", "\"" + "mobile.securitypay.pay" + "\"");
        sParaTemp.put("payment_type", "\"" + payment_type + "\"");
        sParaTemp.put("_input_charset", "\"" + AlipayConfig.input_charset + "\"");

        return AlipaySubmit.buildRequest(sParaTemp);
    }


    /**
     * @param out_trade_no 订单号
     * @param subject      商品名称
     * @param total_fee    总价格
     * @param body         商品详情商品详情
     * @param notify_url   回调通知页面
     **/
    public String webPay(String out_trade_no, String subject, String total_fee, String body, String notify_url, String return_url) {
        String payment_type = "1";
        Map<String, String> sParaTemp = new HashMap<String, String>();
        sParaTemp.put("partner", AlipayConfig.partner);
        sParaTemp.put("seller_id", AlipayConfig.partner);
        sParaTemp.put("out_trade_no", out_trade_no);
        sParaTemp.put("subject", subject);
        sParaTemp.put("body", body);
        sParaTemp.put("total_fee", total_fee);
        sParaTemp.put("notify_url", notify_url);
        sParaTemp.put("return_url", return_url);
        sParaTemp.put("show_url", AlipayConfig.show_url);
        sParaTemp.put("service", "create_direct_pay_by_user");
        sParaTemp.put("payment_type", payment_type);
        sParaTemp.put("_input_charset", AlipayConfig.input_charset);
        sParaTemp.put("anti_phishing_key", "");
        sParaTemp.put("exter_invoke_ip", "");
        Set keySet = sParaTemp.keySet();
        return WebAlipaySubmit.buildRequest(sParaTemp, "get", "确认");
    }

    /**
     * @param out_trade_no 订单号
     * @param subject      商品名称
     * @param total_fee    总价格
     * @param body         商品详情商品详情
     * @param notify_url   回调通知页面
     **/
    public String scanPay1(String out_trade_no, String subject, String total_fee, String body, String notify_url) {
        String payment_type = "1";
        Map<String, String> sParaTemp = new HashMap<String, String>();
        sParaTemp.put("app_id", "\"" + AlipayConfig.appId + "\"");
        sParaTemp.put("method", "\"alipay.trade.precreate\"");
        sParaTemp.put("format", "\"JSON\"");
        sParaTemp.put("out_trade_no", "\"" + out_trade_no + "\"");
        sParaTemp.put("subject", "\"" + subject + "\"");
        sParaTemp.put("sign_type", "\"RSA\"");
        sParaTemp.put("body", "\"" + body + "\"");
        sParaTemp.put("total_fee", "\"" + total_fee + "\"");
        sParaTemp.put("notify_url", "\"" + notify_url + "\"");
        sParaTemp.put("_input_charset", "\"" + AlipayConfig.input_charset + "\"");

        return AlipaySubmit.buildRequest(sParaTemp);
    }

    /**
     * 取现操作
     *
     * @param out_trade_no 用户退款订单号
     */
    public void withdraw(String out_trade_no) {

    }

    /**
     * 退款操作
     *
     * @return 返回支付宝有密支付的url
     */
    public String refund(List<RefundDetailData> list) {
        Refund refund = new Refund();
        String refundUrl = refund.builderDetailData(list);//构造单笔数据集合以及签名
        return refundUrl;
    }
}
